//
//  UITableViewCell+Utility.m
//  LXVolunteer
//
//  Created by 李涛 on 15/5/29.
//  Copyright (c) 2015年 lexue. All rights reserved.
//

#import "UITableViewCell+Utility.h"
#import "HBaseLineView.h"

#define MarginLeft          (IS_IPHONE_6P ? 20 : 16)

@implementation UITableViewCell (Utility)


- (void)setFullLineWith:(UITableView *)tableView IndexPath:(NSIndexPath *)indexPath CellHeight:(CGFloat)cellHeight IsNeedSelected:(BOOL)isNeedSelected{
    [self setMarginLineWith:tableView IndexPath:indexPath CellHeight:cellHeight Margin:0 IsNeedSelected:isNeedSelected];
}


- (void)setMarginLineWith:(UITableView *)tableView IndexPath:(NSIndexPath *)indexPath CellHeight:(CGFloat)cellHeight Margin:(CGFloat)marign IsNeedSelected:(BOOL)isNeedSelected{
    
    [self setMarginLineWith:tableView IndexPath:indexPath CellHeight:cellHeight Margin:marign IsNeedSelected:isNeedSelected IsAllMargin:NO];

}

- (void)setMarginLineWith:(UITableView *)tableView IndexPath:(NSIndexPath *)indexPath CellHeight:(CGFloat)cellHeight Margin:(CGFloat)marign IsNeedSelected:(BOOL)isNeedSelected IsAllMargin:(BOOL)isAllMargin{

    if (cellHeight <= 0) {
        if ([tableView.delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)]) {
            cellHeight = [tableView.delegate tableView:tableView heightForRowAtIndexPath:indexPath];
        }else{
            cellHeight = CGRectGetHeight(self.bounds);
        }
    }
    
    marign = marign >= 0 ? marign : MarginLeft;
    
    HBaseLineView * topLineView = (HBaseLineView *)[self.contentView viewWithTag:TopLineTag];
    if (!topLineView) {
        topLineView = [[HBaseLineView alloc] init];
        topLineView.tag = TopLineTag;
        [self.contentView addSubview:topLineView];
    }
    if (indexPath.row == 0) {
        
        if (isAllMargin) {
            topLineView.frame = CGRectMake(marign, 0, CGRectGetWidth(tableView.bounds) - marign, LineHeight);
        }
        else{
            topLineView.frame = CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), LineHeight);
        }
        topLineView.hidden = NO;
        [self.contentView bringSubviewToFront:topLineView];
    }
    else{
        topLineView.hidden = YES;
    }
    
    HBaseLineView * bottomLineView = (HBaseLineView *)[self.contentView viewWithTag:BottomLineTag];
    if (!bottomLineView) {
        bottomLineView = [[HBaseLineView alloc] init];
        bottomLineView.tag = BottomLineTag;
        [self.contentView addSubview:bottomLineView];
    }
    
    NSInteger count = 0;
    if ([tableView.dataSource respondsToSelector:@selector(tableView:numberOfRowsInSection:)]) {
        count = [tableView.dataSource tableView:tableView numberOfRowsInSection:[indexPath section]];
    }
    
    if (isAllMargin) {
        bottomLineView.frame = CGRectMake(marign, cellHeight - LineHeight, CGRectGetWidth(tableView.bounds) - marign, LineHeight);
    }
    else{
        if (indexPath.row == count - 1) {
            bottomLineView.frame = CGRectMake(0, cellHeight - LineHeight, CGRectGetWidth(tableView.bounds), LineHeight);
        }
        else{
            bottomLineView.frame = CGRectMake(marign, cellHeight - LineHeight, CGRectGetWidth(tableView.bounds) - marign, LineHeight);
        }
    }
    
    [self.contentView bringSubviewToFront:bottomLineView];
    
    if (!self.selectedBackgroundView || [NSStringFromClass(self.selectedBackgroundView.class) isEqualToString:@"UITableViewCellSelectedBackground"]) {
        UIView * selectedView = [[UIView alloc] init];
        selectedView.backgroundColor = isNeedSelected ? [UIColor clearColor] : [UIColor cellHighLightColor];
        selectedView.tag = SelectedViewTag;
        self.selectedBackgroundView = selectedView;
    }
    else{
        if (self.selectedBackgroundView.tag == SelectedViewTag) {
            self.selectedBackgroundView.backgroundColor = isNeedSelected ? [UIColor clearColor] : [UIColor cellHighLightColor];
        }
    }
    
    if (isNeedSelected) {
        HBaseLineView * selectedLineView = (HBaseLineView *)[self.selectedBackgroundView viewWithTag:SelectedLineTag];
        if (!selectedLineView) {
            selectedLineView = [[HBaseLineView alloc] init];
            selectedLineView.backgroundColor = [UIColor baseColor];
            selectedLineView.tag = SelectedLineTag;
            [self.selectedBackgroundView addSubview:selectedLineView];
        }
        if (isAllMargin) {
            selectedLineView.frame = CGRectMake(marign, cellHeight - LineHeight, CGRectGetWidth(tableView.bounds) - marign, LineHeight);
        }
        else{
            
            if (indexPath.row == count - 1) {
                selectedLineView.frame = CGRectMake(0, cellHeight - LineHeight, CGRectGetWidth(tableView.bounds), LineHeight);
            }
            else{
                selectedLineView.frame = CGRectMake(marign, cellHeight - LineHeight, CGRectGetWidth(tableView.bounds) - marign, LineHeight);
            }
            
        }
        selectedLineView.hidden = NO;
    }
    else{
        HBaseLineView * selectedLineView = (HBaseLineView *)[self.selectedBackgroundView viewWithTag:SelectedLineTag];
        selectedLineView.hidden = YES;
    }

}


@end
